using ApiTest.Api.Entity;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using System;
using ApiTest.Api.Db;

namespace ApiTest.Api.Repository
{
    public class EfRepository<T> : IRepository<T> where T : BaseInit
    {

        private DbInit _db;

        public EfRepository(DbInit db)
        {
            _db = db;
        }


        //DbSet：表示可用于创建、读取、更新和删除操作的实体集
        private DbSet<T> _entity;


        protected DbSet<T> Entity
        {
            get
            {
                if(_entity == null)
                {
                    _entity = _db.Set<T>();
                }
                return _entity;
            }
        }
        public IQueryable<T> Table
        {
            get
            {
                return Entity.AsQueryable<T>();
            }
        }



        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        public void Delete(object id)
        {
            //通过 id 找到要删除的 Id
            var t = Entity.Where(x=>x.Id == (int)id).FirstOrDefault();
            //删除
            _db.Remove(t);
            //保存
            _db.SaveChanges();
        }



        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        public void DeleteBatch(IEnumerable<object> ids)
        {
            var idList = new List<int>();

            //遍历所有 id 把要删除的添加到定义的空集合
            foreach (var item in ids)
            {
                var list = (int)item;
                idList.Add(list);
            }

            //Contains：查询条件判断
            var il = Entity.Where(x=>idList.Contains(x.Id)).ToList();

            _db.RemoveRange(il);

            _db.SaveChanges();
        }



        /// <summary>
        /// 通过 id 查找
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public T GetById(object id)
        {
            return Entity.Where(x=>x.Id == (int)id).FirstOrDefault();
        }



        /// <summary>
        /// 插入
        /// </summary>
        /// <param name="entity"></param>
        public void Insert(T entity)
        {
            // 判断提供的实体对象是否为空
            if(entity == null)
            {
                throw new ArgumentNullException();
            }


            //插入数据
            entity.IsActived = true;
            entity.IsDeleted = false;
            entity.CreatedTime = DateTime.Now;
            entity.UpdatedTime = DateTime.Now;
            entity.DisplayOrder = 0;

            Entity.Add(entity);

            _db.SaveChanges();
        }



        /// <summary>
        /// 异步插入
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task InsertAsync(T entity)
        {
            // 判断提供的实体对象是否为空
            if(entity == null)
            {
                //如果实体对象为null，则抛出异常
                throw new ArgumentNullException();
            }


            //插入数据
            entity.IsActived = true;
            entity.IsDeleted = false;
            entity.CreatedTime = DateTime.Now;
            entity.UpdatedTime = DateTime.Now;
            entity.DisplayOrder = 0;

            await Entity.AddAsync(entity);

            await _db.SaveChangesAsync();
        }



        /// <summary>
        /// 批量插入
        /// </summary>
        /// <param name="entities"></param>
        public void InsertBatch(IEnumerable<T> entities)
        {
            //遍历提供的实体对象集合
            foreach (var entity in entities)
            {
                entity.IsActived = true;
                entity.IsDeleted = false;
                entity.CreatedTime = DateTime.Now;
                entity.UpdatedTime = DateTime.Now;
                entity.DisplayOrder = 0;
            }
            Entity.AddRange(entities);
            _db.SaveChanges();
        }



        /// <summary>
        /// 异步批量插入
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        public async Task InsertBatchAsync(IEnumerable<T> entities)
        {
            //遍历提供的实体对象集合
            foreach (var entity in entities)
            {
                entity.IsActived = true;
                entity.IsDeleted = false;
                entity.CreatedTime = DateTime.Now;
                entity.UpdatedTime = DateTime.Now;
                entity.DisplayOrder = 0;
            }
            await Entity.AddRangeAsync(entities);
            await _db.SaveChangesAsync();
        }



        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity"></param>
        public void Update(T entity)
        {
            if(entity == null)
            {
                //entity为null时抛出异常
                throw new ArgumentNullException(nameof(entity));
            }

            entity.UpdatedTime = DateTime.Now;
            _db.SaveChanges();
        }



        /// <summary>
        /// 批量修改
        /// </summary>
        /// <param name="entities"></param>
        public void UpdateBatch(IEnumerable<T> entities)
        {
            foreach (var entity in entities)
            {
                entity.UpdatedTime = DateTime.Now;
            }

            Entity.UpdateRange(entities);
            _db.SaveChanges();
        }

        void IRepository<T>.DeleteBatch(IEnumerable<T> entities)
        {
            throw new NotImplementedException();
        }
    }
}